<?php
/**
 * @name /www/account.php
 * @author Michael Foss <michael@mikefoss.com>
 * @version 2007.07.02
 *
 * This is the home page for the site template.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
ReqLib('/lib/private/User.php');
ReqLib('/lib/private/Notification.php');

/**
 * Defines preload page functionality;
 * this happens after DefineParams,
 * but before anything else.
 */
function DefinePreload() {
  if (!$_SESSION['userId']) {
    header('location: ' . _webroot('/login_register.php?pagereq=/account.php'));
    die();
  }
  // Insert page preload functionality here.
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  // Page content goes here.
  ?>
<h1>Account</h1>
<?
  $user = new User();
  $user->Load($_SESSION['userId']);
  $isAdmin = $user->HasRole('Administrator');
  $isDonor = $user->HasRole('donator');
  $links = array();
  /*
   * Links are arranged as an associative array of links;
   * the keys of the $links array represent the headers;
   * the values of the $links array are arrays
   * that contain a 2-celled 1-dimensional array
   * representing the link and the text.
   * Headers & links are ordered in a FIFO-basis.
   *
   * For example, consider the following $links variable:
   *
   * $links = array(
   *   'Foo' => array(
   *     array('alpha.php', 'A'),
   *     array('beta.php', 'B'),
   *     array('delta.php', 'D')
   *   ),
   *   'Bar' => array(
   *     array('primary.php', '1'),
   *     array('secondary.php', '2')
   *   )
   * );
   *
   * This would produce the output:
   *
   * <h2>Foo</h2>
   * <ul>
   *   <li><a href="/alpha.php">A</a></li>
   *   <li><a href="/beta.php">B</a></li>
   *   <li><a href="/delta.php">D</a></li>
   * </ul>
   * <h2>Bar</h2>
   * <ul>
   *   <li><a href="/primary.php">1</a></li>
   *   <li><a href="/secondary.php">2</a></li>
   * </ul>
   */
  $links['Navigation'] = array(
    array('/index.php', 'Home')
  );
  if ($isAdmin || $isDonor) {
    $links['Donations'] = array(
      array('/donations/', 'Donate'),
      array('/donations/', 'View Past Donations')
    );
  }
  if (!$isDonor)
    $links['Donations'][] = array('/become-donor.php', 'Become Donor');
  elseif (!$user->HasDonationHistory())
    $links['Donations'][] = array('/stop-donating.php', 'Stop Being A Donor');

  $links['Account'] = array(
    array('/logout.php', 'Logout'),
    array('/EditMyInfo.php', 'Change My Information'),
    array('/EditLoginInfo.php', 'Change Login Information')
  );
  if ($isAdmin) {
    $links['Administrative'] = array(
      array('/Item.php', 'Items'),
      array('/neededItems.php', 'Needed Items'),
      array('/orphanages.php', 'Orphanages'),
      array('/users.php', 'Users'),
	  array('/Tag.php', 'Tags'),
	  array('/unverifiedItem_report.php', 'Report on Unverified Items')
    );
    $links['Developers'] = array(
      array('/unit-tests/', 'Unit Tests (Developers Only)')
    );
  }

  // Display notification widget if admin
  if ($isAdmin) {
    println('<div style="float: right; width: 600px;">');
    Notification::DisplayWidget();
    println('</div>');
  }

  // Generate content
  foreach ($links as $header => $sectionLinks) {
    println('<h2>' . htmlspecialchars($header) . '</h2>');
    println('<ul>');
    foreach ($sectionLinks as $link)
      println('<li><a href="' . _webroot($link[0]) . '">' . htmlspecialchars($link[1]) . '</a></li>');
    println('</ul>');
  }
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');